package com.ssb.plugins;

import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.DataMap;
import com.apache.tools.StrUtil;
import com.ssb.service.check.CheckedFrontSysService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 产品标签关系查产品列表
 * @author: fpchang
 * @date: Created in 2020/2/11
 */
public class TagProListPluginImpl implements CustomMethodPlugin {

    @Override
    public Object beforeInvoke(Map<String, Object> map) {

        String orgId = String.valueOf(map.get("uctUser.w_orgId"));
        if(Validator.isNull(orgId)){
            throw new BusinessException("【uctUser.w_orgId】为空，请检验请求参数。");
        }
        String sql= getSql(orgId);
        List<DataMap> list = (List)IusPluginUtil.getInstance().select(sql, 0, 0);
        List<Map> tagList = new ArrayList<Map>();
        Map tempMap = new HashMap();
        List tempProList ;
        for (DataMap dataMap:list) {
            Map tagMap = new HashMap<String,Object>();
            String tagId = String.valueOf(dataMap.get("tagId"));
            String infoId = String.valueOf(dataMap.get("infoId"));
            if(Validator.isNotNull(infoId)){
                if(!tempMap.containsKey(tagId)){
                    tempProList = new ArrayList();
                    tagMap.put("tagCname",dataMap.get("tagCname"));
                    tagMap.put("sort",dataMap.get("sort"));
                    tagMap.put("tagDescribe",dataMap.get("tagDescribe"));
                    tagMap.put("tagId",tagId);
                    dataMap.remove("tagCname");
                    dataMap.remove("sort");
                    dataMap.remove("tagDescribe");
                    dataMap.remove("tagid");
                    tagList.add(tagMap);
                    tempProList.add(dataMap);
                    tempMap.put(tagId,tempProList);
                }else{
                    tempProList = (List) tempMap.get(tagId);
                    dataMap.remove("tagCname");
                    dataMap.remove("sort");
                    dataMap.remove("tagDescribe");
                    dataMap.remove("tagid");
                    tempProList.add(dataMap);
                    tempMap.put(tagId, tempProList);
                }
            }
        }
        for (Map tagMap:tagList) {
            tagMap.put("proList",tempMap.get(tagMap.get("tagId")));
        }
        map.put(CustomMethodPlugin.resultMark,"查询成功");
        map.put(CustomMethodPlugin.pluginInnerReturn,tagList);
        return null;
    }

    private String getSql(String orgId){
        StringBuilder sb = new StringBuilder();
        sb.append("select sipt.tag_cname,sipt.sort,sipt.info_id tag_id,sipt.tag_describe,sp.* FROM ss_insur_pro_tag_relation siptr ");
        sb.append(" LEFT JOIN ss_insur_pro_tag sipt ON siptr.insur_tag_id=sipt.info_id ");
        if("0".equals(orgId)){
            sb.append(" LEFT JOIN (SELECT si.*,MIN(CONVERT(sl.premium,SIGNED)) AS min_premium from ss_insur_pro si,ss_insur_pro_limit sl WHERE si.info_id=sl.pro_id AND si.up_flag ='T' GROUP BY si.info_id )  sp" );
        }else{
            sb.append(" LEFT JOIN (SELECT sip.* FROM (SELECT si.*,MIN(CONVERT(sl.premium,SIGNED)) AS min_premium from ss_insur_pro si,ss_insur_pro_limit sl WHERE si.info_id=sl.pro_id AND si.up_flag ='T' GROUP BY si.info_id ) sip , ss_org_pro_relation sopr WHERE sip.info_id = sopr.pro_id ");
            sb.append(" AND sopr.user_id='"+orgId+"') sp ");
        }
        sb.append(" ON siptr.insur_pro_id=sp.info_id ");
        sb.append(" WHERE sipt.enabled='T' ");
        sb.append(" order by CONVERT(sipt.sort,SIGNED)");
        return sb.toString();
    }

    @Override
    public Object doInvoke(Map<String, Object> map) {
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> map) {
        return null;
    }
}
